<?php

include_once("../dao/conexao_class.php");
include_once("../model/sala_model_class.php");
include_once("../controller/predio_controller_class.php");


class SalaDAO {

    private $insert = "INSERT INTO sala(pre_id, sal_identificacao, sal_andar) values(?, ?, ?);";
    private $update = "UPDATE sala SET pre_id, sal_identificacao = ?, sal_andar WHERE sal_id = ?;";
    private $delete = "DELETE FROM sala WHERE sal_id = ?;";
    private $select = "SELECT * FROM sala;";
    private $select_by_id = "SELECT * FROM sala WHERE sal_id = ?;";
    private $select_by_predio = "SELECT * FROM sala WHERE pre_id = ?;";
    private $select_by_id_sala = "SELECT * FROM sala WHERE pre_id = ? AND sal_id = ?;";

    function insert($objSala) {
        //conectando ao banco
        $objConn = new Conexao();
        $conn = $objConn->conecta();
//        var_dump($conn);
        //criando o preparedStatment
        if ($stm = $conn->prepare($this->insert)) {

            $stm->bind_param("isi", $objSala->getPredio(), $objSala->getIdentificacao(), $objSala->getAndar());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        } else {
            return false;
        }
    }

    function update($objSala) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->update)) {

            $stm->bind_param("isii", $objSala->getPredio(), $objSala->getIdentificacao(), $objSala->getAndar(), $objSala->getId());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function delete($id) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->delete)) {

            $stm->bind_param("i", $id);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function findAllSala() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $salM = new SalaModel();
        $lista = array();
        
        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $predio, $identificacao, $andar);


            //para cada resultado encontrado
            $i = 0;
            while ($stm->fetch()) {
                
                $salM = new SalaModel();
                $objSala = new PredioController();
                
                $predio = $objSala->selecionarPredioPorId($predio);
                
                $salM->setId($id);
                $salM->setPredio($predio);
                $salM->setIdentificacao($identificacao);
                $salM->setAndar($andar);
                
                $lista[$i++] = $salM;
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findSalaById($sala) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $salM = new SalaModel();
        
        if ($stm = $conn->prepare($this->select_by_id)) {
            
            $stm->bind_param("i", $sala);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $predio, $identificacao, $andar);


            //para cada resultado encontrado
            
            while ($stm->fetch()) {
                $salM = new SalaModel();
                $objSala = new PredioController();
                
                $predio = $objSala->selecionarPredioPorId($predio);
                
                $salM->setId($id);
                $salM->setPredio($predio);
                $salM->setIdentificacao($identificacao);
                $salM->setAndar($andar);
                
            }

            $conn->close();
            return $salM;
        }
    }
    
    function findSalasByPredio($predio) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $salM = new SalaModel();
        $lista = array();
        if ($stm = $conn->prepare($this->select_by_predio)) {
            
            $stm->bind_param("i", $predio);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $pred, $identificacao, $andar);


            //para cada resultado encontrado
            $i = 0;
            while ($stm->fetch()) {
                $salM = new SalaModel();
                $objSala = new PredioController();
                
                $pred = $objSala->selecionarPredioPorId($pred);
                
                $salM->setId($id);
                $salM->setPredio($pred);
                $salM->setIdentificacao($identificacao);
                $salM->setAndar($andar);
                
                $lista[$i++] = $salM;
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findSalasByIdJSON($sala) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $lista = array();
        if ($stm = $conn->prepare($this->select_by_id)) {
            
            $stm->bind_param("i", $sala);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $predio, $identificacao, $andar);


            //para cada resultado encontrado
            
            while ($stm->fetch()) {
//                echo "Id: ".$id." Descrição: ".$descricao."<br>";
                $lista['salas'][] = array(
                    'id' => $id,
                    'predio' => $predio,
                    'identificacao' => $identificacao,
                    'andar' => $andar
                );
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findSalasByPredioJSON($sala) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $lista = array();
        if ($stm = $conn->prepare($this->select_by_predio)) {
            
            $stm->bind_param("i", $sala);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $predio, $identificacao, $andar);


            //para cada resultado encontrado
            
            while ($stm->fetch()) {
//                echo "Id: ".$id." Descrição: ".$descricao."<br>";
                $lista['salas'][] = array(
                    'id' => $id,
                    'predio' => $predio,
                    'identificacao' => $identificacao,
                    'andar' => $andar
                );
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findAllSalaJSON() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $lista = array();
        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $predio, $identificacao, $andar);


            //para cada resultado encontrado
            
            while ($stm->fetch()) {
//                echo "Id: ".$id." Descrição: ".$descricao."<br>";
                $lista['salas'][] = array(
                    'id' => $id,
                    'predio' => $predio,
                    'identificacao' => $identificacao,
                    'andar' => $andar
                );
            }

            $conn->close();
            return $lista;
        }
    }
    
    function existe($sala, $predio) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $lista = array();
        if ($stm = $conn->prepare($this->select_by_id_sala)) {
            
            $stm->bind_param("ii", $sala,$predio);
            
            $stm->execute();

            while ($stm->fetch()) {
                $conn->close();
                return true;
            }

            $conn->close();
            return false;
        }
    }
    
    
}

//$teste = new SalaDAO();
//$sal = new SalaModel();
////$sal->setId(3);
//$sal->setDescricao("Administraçao");
//
////$sal = 3;
//if($teste->insert($sal)){
//    printf("Deletado com sucesso!");
//}else{
//    printf("Erro ao deletar!");
//}
//
//var_dump($teste->findAllSala());

